package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.VipArticle;
import co.yixiang.modules.shop.service.YxStoreCategoryService;
import co.yixiang.modules.shop.service.dto.YxStoreCategoryDTO;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.shop.repository.VipArticleRepository;
import co.yixiang.modules.shop.service.VipArticleService;
import co.yixiang.modules.shop.service.dto.VipArticleDto;
import co.yixiang.modules.shop.service.dto.VipArticleQueryCriteria;
import co.yixiang.modules.shop.service.mapper.VipArticleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author lijunze
* @date 2020-05-09
*/
@Service
//@CacheConfig(cacheNames = "vipArticle")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class VipArticleServiceImpl implements VipArticleService {

    private final YxStoreCategoryService yxStoreCategoryService;

    private final VipArticleRepository vipArticleRepository;

    private final VipArticleMapper vipArticleMapper;

    public VipArticleServiceImpl(YxStoreCategoryService yxStoreCategoryService, VipArticleRepository vipArticleRepository, VipArticleMapper vipArticleMapper) {
        this.yxStoreCategoryService = yxStoreCategoryService;


        this.vipArticleRepository = vipArticleRepository;
        this.vipArticleMapper = vipArticleMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(VipArticleQueryCriteria criteria, Pageable pageable){
        Page<VipArticle> page = vipArticleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        for (VipArticle vipArticle : page) {
            YxStoreCategoryDTO byId = yxStoreCategoryService.findById(Integer.parseInt(vipArticle.getClassifyId()));
            //v.setClassifyId(byId.getCateName());
            vipArticle.setClassifyName(byId.getCateName());
        }
        System.out.println(page);
        return PageUtil.toPage(page.map(vipArticleMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<VipArticleDto> queryAll(VipArticleQueryCriteria criteria){
        return vipArticleMapper.toDto(vipArticleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public VipArticleDto findById(Integer id) {
        VipArticle vipArticle = vipArticleRepository.findById(id).orElseGet(VipArticle::new);
        ValidationUtil.isNull(vipArticle.getId(),"VipArticle","id",id);
        return vipArticleMapper.toDto(vipArticle);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public VipArticleDto create(VipArticle resources) {
        resources.setIsDel(2);
        resources.setCreateTime(LocalDateTime.now());
        return vipArticleMapper.toDto(vipArticleRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(VipArticle resources) {
        VipArticle vipArticle = vipArticleRepository.findById(resources.getId()).orElseGet(VipArticle::new);
        ValidationUtil.isNull( vipArticle.getId(),"VipArticle","id",resources.getId());
        vipArticle.copy(resources);
        vipArticleRepository.save(vipArticle);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            vipArticleRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<VipArticleDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (VipArticleDto vipArticle : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("标题", vipArticle.getTitle());
            map.put("图片", vipArticle.getImage());
            map.put("描述简介", vipArticle.getDescribe());
            map.put("正文", vipArticle.getContent());
            map.put("是否删除", vipArticle.getIsDel());
            map.put("添加时间", vipArticle.getCreateTime());
            map.put("vip等级要求", vipArticle.getVipLv());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}